package com.ccubee.data.transfer.web;

import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.ccubee.data.transfer.model.TransferRequest;
import com.ccubee.data.transfer.transfer.ITransfer;
import com.ccubee.data.transfer.transfer.monitor.DataTransferMonitor;
import com.ccubee.data.transfer.transfer.monitor.MonitorProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * 数据传输处理
 *
 * @Author ycf
 * @Date 2022/11/15 22:31
 * @Version 1.0
 */
@RestController
@RequestMapping("transfer")
public class TransferController {

    @Resource
    private ITransfer iTransfer;

    /**
     * 开始任务
     *
     * @param request 参数
     */
    @GetMapping("start")
    public Set<String> start(TransferRequest request) {
        if (StringUtils.isEmpty(request.getInstance())) {
            throw ExceptionUtils.mpe("实例必传");
        }
        return iTransfer.start(request);
    }

    /**
     * 结束任务
     *
     * @param keys 任务实例
     */
    @GetMapping("stop")
    public void end(@RequestParam(defaultValue = "") String keys) {
        iTransfer.stop(Set.of(keys.split(",")));
    }

    /**
     * 监控数据
     *
     * @return 结果
     */
    @GetMapping("monitor")
    public Map<String, Map<String, Map<String, MonitorProperties>>> monitor() {
        return DataTransferMonitor.monitor();
    }
}
